/*
 * RunTestJPanel.java
 *
 * Created on Oct 3, 2010, 9:17:46 PM
 */
package ws.performancetest.app.gui.panel;

import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JSpinner;
import ws.performancetest.api.Test;
import ws.performancetest.api.TestSuite;
import ws.performancetest.app.gui.Application;
import ws.performancetest.app.gui.DBConnectionListener;
import ws.performancetest.app.gui.dialog.ErrorDialog;
import ws.performancetest.app.gui.dialog.RunTestDialog;
import ws.performancetest.app.gui.dialog.WarningDialog;
import ws.performancetest.app.gui.strings.AppStrings;
import ws.utils.jdbc.DBConnection;

/**
 * This panel provides the user a graphical interface to run the tests.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class RunTestJPanel extends javax.swing.JPanel {

  /** Combo box model for test suite. */
  private DefaultComboBoxModel testSuiteComboBoxModel = new DefaultComboBoxModel();
  /** List model to the performance test list. */
  private DefaultListModel testListModel = new DefaultListModel();
  /** List model to the thread amount list. */
  private DefaultListModel threadAmountListModel = new DefaultListModel();
  /** List model to the cycle amount list. */
  private DefaultListModel cycleListModel = new DefaultListModel();

  /** Creates new form RunTestJPanel */
  public RunTestJPanel() {
    try {
      setTestSuiteComboBox();
      initComponents();
      setTestList();
      DBConnectionListener.addComponent(btLoad);
      DBConnectionListener.addComponent(btSaveToDB);
      DBConnectionListener.addComponent(btRunTests);
    } catch (Exception ex) {
      ErrorDialog.run(AppStrings.createExceptionMessage(ex));
      ex.printStackTrace();
    }
  }

  /**
   * Get the test group list from the database and display it.
   */
  public final void setTestSuiteComboBox() {
    testSuiteComboBoxModel.removeAllElements();
    for (TestSuite suite : Application.getTestSuites()) {
      testSuiteComboBoxModel.addElement(suite);
    }
  }

  private void setTestList() {
    TestSuite ts = (TestSuite) cbxTestSuite.getSelectedItem();
    testListModel.removeAllElements();
    for (Test method : ts.getTestList()) {
      testListModel.addElement(method);
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    pnlTestSuiteSelection = new javax.swing.JPanel();
    cbxTestSuite = new javax.swing.JComboBox();
    pnlTestSelection = new javax.swing.JPanel();
    jScrollPane2 = new javax.swing.JScrollPane();
    lstTestList = new javax.swing.JList();
    btSelectAllTest = new javax.swing.JButton();
    btSelectNoneTest = new javax.swing.JButton();
    pnlConfiguration = new javax.swing.JPanel();
    btRunTests = new javax.swing.JButton();
    btLoad = new javax.swing.JButton();
    pnlThreadAmount = new javax.swing.JPanel();
    lbThreadAmount = new javax.swing.JLabel();
    jScrollPane3 = new javax.swing.JScrollPane();
    lstThreadAmount = new javax.swing.JList();
    spAmount = new javax.swing.JSpinner();
    btAddAmount = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    lbRep = new javax.swing.JLabel();
    spCycle = new javax.swing.JSpinner();
    jScrollPane1 = new javax.swing.JScrollPane();
    lstCycleAmount = new javax.swing.JList();
    btAddCycle = new javax.swing.JButton();
    lbSample = new javax.swing.JLabel();
    spSamples = new javax.swing.JSpinner();
    btSaveToDB = new javax.swing.JToggleButton();

    pnlTestSuiteSelection.setBorder(javax.swing.BorderFactory.createTitledBorder("Test Suite selection"));

    cbxTestSuite.setModel(testSuiteComboBoxModel);
    cbxTestSuite.setSelectedItem(testSuiteComboBoxModel);
    cbxTestSuite.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cbxTestSuiteActionPerformed(evt);
      }
    });

    pnlTestSelection.setBorder(javax.swing.BorderFactory.createTitledBorder("Test"));

    jScrollPane2.setBorder(null);

    lstTestList.setModel(testListModel);
    jScrollPane2.setViewportView(lstTestList);

    btSelectAllTest.setText("All");
    btSelectAllTest.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btSelectAllTestActionPerformed(evt);
      }
    });

    btSelectNoneTest.setText("None");
    btSelectNoneTest.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btSelectNoneTestActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout pnlTestSelectionLayout = new javax.swing.GroupLayout(pnlTestSelection);
    pnlTestSelection.setLayout(pnlTestSelectionLayout);
    pnlTestSelectionLayout.setHorizontalGroup(
      pnlTestSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlTestSelectionLayout.createSequentialGroup()
        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(pnlTestSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(btSelectAllTest, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
          .addComponent(btSelectNoneTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addContainerGap())
    );
    pnlTestSelectionLayout.setVerticalGroup(
      pnlTestSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlTestSelectionLayout.createSequentialGroup()
        .addComponent(btSelectAllTest)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(btSelectNoneTest)
        .addContainerGap(177, Short.MAX_VALUE))
      .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout pnlTestSuiteSelectionLayout = new javax.swing.GroupLayout(pnlTestSuiteSelection);
    pnlTestSuiteSelection.setLayout(pnlTestSuiteSelectionLayout);
    pnlTestSuiteSelectionLayout.setHorizontalGroup(
      pnlTestSuiteSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlTestSuiteSelectionLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(pnlTestSuiteSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(pnlTestSelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(cbxTestSuite, 0, 297, Short.MAX_VALUE))
        .addContainerGap())
    );
    pnlTestSuiteSelectionLayout.setVerticalGroup(
      pnlTestSuiteSelectionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlTestSuiteSelectionLayout.createSequentialGroup()
        .addComponent(cbxTestSuite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(pnlTestSelection, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pnlConfiguration.setBorder(javax.swing.BorderFactory.createTitledBorder("Configuration"));

    btRunTests.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ws/performancetest/app/gui/imgs/run-15.png"))); // NOI18N
    btRunTests.setText("Run Tests");
    btRunTests.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btRunTestsActionPerformed(evt);
      }
    });

    btLoad.setText("Load Config from DB");
    btLoad.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btLoadActionPerformed(evt);
      }
    });

    lbThreadAmount.setText("Thread Amounts");

    lstThreadAmount.setModel(threadAmountListModel);
    lstThreadAmount.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyReleased(java.awt.event.KeyEvent evt) {
        lstThreadAmountKeyReleased(evt);
      }
    });
    jScrollPane3.setViewportView(lstThreadAmount);

    spAmount.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(5)));

    btAddAmount.setText("Add");
    btAddAmount.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btAddAmountActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout pnlThreadAmountLayout = new javax.swing.GroupLayout(pnlThreadAmount);
    pnlThreadAmount.setLayout(pnlThreadAmountLayout);
    pnlThreadAmountLayout.setHorizontalGroup(
      pnlThreadAmountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlThreadAmountLayout.createSequentialGroup()
        .addComponent(spAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(btAddAmount))
      .addComponent(lbThreadAmount)
      .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
    );
    pnlThreadAmountLayout.setVerticalGroup(
      pnlThreadAmountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlThreadAmountLayout.createSequentialGroup()
        .addComponent(lbThreadAmount)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(pnlThreadAmountLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(spAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(btAddAmount))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
    );

    lbRep.setText("Cycle Amounts");

    spCycle.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(100), Integer.valueOf(1), null, Integer.valueOf(10)));

    lstCycleAmount.setModel(cycleListModel);
    lstCycleAmount.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyReleased(java.awt.event.KeyEvent evt) {
        lstCycleAmountKeyReleased(evt);
      }
    });
    jScrollPane1.setViewportView(lstCycleAmount);

    btAddCycle.setText("Add");
    btAddCycle.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btAddCycleActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
          .addComponent(spCycle, javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(lbRep, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(btAddCycle, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addComponent(lbRep)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(spCycle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(btAddCycle))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
    );

    lbSample.setText("Samples");

    spSamples.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(10), Integer.valueOf(1), null, Integer.valueOf(5)));

    btSaveToDB.setSelected(true);
    btSaveToDB.setText("Save Config to DB");

    javax.swing.GroupLayout pnlConfigurationLayout = new javax.swing.GroupLayout(pnlConfiguration);
    pnlConfiguration.setLayout(pnlConfigurationLayout);
    pnlConfigurationLayout.setHorizontalGroup(
      pnlConfigurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlConfigurationLayout.createSequentialGroup()
        .addContainerGap()
        .addGroup(pnlConfigurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlConfigurationLayout.createSequentialGroup()
            .addComponent(lbSample, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(spSamples, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(19, 19, 19))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlConfigurationLayout.createSequentialGroup()
            .addGroup(pnlConfigurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(pnlThreadAmount, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .addComponent(btLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
        .addGroup(pnlConfigurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(btRunTests, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(btSaveToDB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addContainerGap())
    );
    pnlConfigurationLayout.setVerticalGroup(
      pnlConfigurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(pnlConfigurationLayout.createSequentialGroup()
        .addGroup(pnlConfigurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(btLoad)
          .addComponent(btSaveToDB))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(pnlConfigurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(pnlThreadAmount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(pnlConfigurationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(lbSample)
          .addComponent(spSamples, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(btRunTests, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(pnlTestSuiteSelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(pnlConfiguration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(pnlTestSuiteSelection, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(pnlConfiguration, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
  }// </editor-fold>//GEN-END:initComponents

  private void btRunTestsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRunTestsActionPerformed
    /* Error checking. */
    if (lstTestList.getSelectedIndices().length == 0) {
      WarningDialog.run("<html>There are no selected tests, please selected one.");
      return;
    }
    if (threadAmountListModel.isEmpty()) {
      WarningDialog.run("You must at least add one 'Thread Amount'.");
      return;
    }
    if (cycleListModel.isEmpty()) {
      WarningDialog.run("You must at least add one 'Cycle Amount'.");
      return;
    }

    try {
      /* Get selected tests. */
      List<Test> selected = new LinkedList<Test>();
      for (Object item : lstTestList.getSelectedValues()) {
        selected.add((Test) item);
      }
      TestSuite testSuite = (TestSuite) cbxTestSuite.getSelectedItem();

      /* Get test parameters. */
      int samples = Integer.parseInt(spSamples.getValue().toString());
      Integer[] threadAmountList = new Integer[threadAmountListModel.getSize()];
      threadAmountListModel.copyInto(threadAmountList);
      Integer[] cycleAmounList = new Integer[cycleListModel.getSize()];
      cycleListModel.copyInto(cycleAmounList);

      /* Set the test suite parameters. */
      testSuite.setSamplesCount(samples);
      testSuite.setThreadAmountList(threadAmountList);
      testSuite.setCycleAmountList(cycleAmounList);
      testSuite.setDBUrl(DBConnection.getDBUrl());

      /* Save configuration to the database. */
      if (btSaveToDB.isSelected()) {
        testSuite.saveConfiguration();
      }

      /* Run the selected tests. */
      (new RunTestDialog(testSuite, selected)).run();
    } catch (Exception ex) {
      ErrorDialog.run(AppStrings.createExceptionMessage(ex));
      ex.printStackTrace();
    }
  }//GEN-LAST:event_btRunTestsActionPerformed

  private void btAddAmountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddAmountActionPerformed
    addToList(threadAmountListModel, spAmount);
}//GEN-LAST:event_btAddAmountActionPerformed

  private void cbxTestSuiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxTestSuiteActionPerformed
    setTestList();
  }//GEN-LAST:event_cbxTestSuiteActionPerformed

  private void lstThreadAmountKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lstThreadAmountKeyReleased
    if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
      /* Delete */
      deleteSelected(threadAmountListModel, lstThreadAmount);
    }
  }//GEN-LAST:event_lstThreadAmountKeyReleased

  private void btSelectNoneTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelectNoneTestActionPerformed
    lstTestList.setSelectedIndices(new int[0]);
  }//GEN-LAST:event_btSelectNoneTestActionPerformed

  private void btSelectAllTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelectAllTestActionPerformed
    int[] indices = new int[testListModel.getSize()];
    for (int i = 0; i < testListModel.getSize(); ++i) {
      indices[i] = i;
    }
    lstTestList.setSelectedIndices(indices);
  }//GEN-LAST:event_btSelectAllTestActionPerformed

  private void deleteSelected(DefaultListModel listModel, JList list) {
    for (Object selected : list.getSelectedValues()) {
      listModel.removeElement(selected);
    }
  }

  private void addToList(DefaultListModel listModel, JSpinner spinner) {
    if ((Integer) spinner.getValue() != 0 && !listModel.contains(spinner.getValue())) {
      listModel.addElement(spinner.getValue());
    }
  }

  private void btLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLoadActionPerformed
    /* Load configuration from the database. */
    TestSuite testSuite = (TestSuite) cbxTestSuite.getSelectedItem();
    try {
      /* If there's no configuration on DB exit. */
      if (!testSuite.loadConfiguration()) {
        return;
      }
    } catch (SQLException ex) {
      ErrorDialog.run(AppStrings.createExceptionMessage(ex));
      ex.printStackTrace();
    }
    /* Copy configuration to GUI components. */
    spSamples.setValue(testSuite.getSamplesCount());
    cycleListModel.removeAllElements();
    for (int amount : testSuite.getCycleAmountList()) {
      cycleListModel.addElement(amount);
    }
    threadAmountListModel.removeAllElements();
    for (int amount : testSuite.getThreadAmountList()) {
      threadAmountListModel.addElement(amount);
    }
  }//GEN-LAST:event_btLoadActionPerformed

  private void btAddCycleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddCycleActionPerformed
    addToList(cycleListModel, spCycle);
  }//GEN-LAST:event_btAddCycleActionPerformed

  private void lstCycleAmountKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lstCycleAmountKeyReleased
    if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
      /* Delete */
      deleteSelected(cycleListModel, lstCycleAmount);
    }
  }//GEN-LAST:event_lstCycleAmountKeyReleased
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btAddAmount;
  private javax.swing.JButton btAddCycle;
  private javax.swing.JButton btLoad;
  private javax.swing.JButton btRunTests;
  private javax.swing.JToggleButton btSaveToDB;
  private javax.swing.JButton btSelectAllTest;
  private javax.swing.JButton btSelectNoneTest;
  private javax.swing.JComboBox cbxTestSuite;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JScrollPane jScrollPane3;
  private javax.swing.JLabel lbRep;
  private javax.swing.JLabel lbSample;
  private javax.swing.JLabel lbThreadAmount;
  private javax.swing.JList lstCycleAmount;
  private javax.swing.JList lstTestList;
  private javax.swing.JList lstThreadAmount;
  private javax.swing.JPanel pnlConfiguration;
  private javax.swing.JPanel pnlTestSelection;
  private javax.swing.JPanel pnlTestSuiteSelection;
  private javax.swing.JPanel pnlThreadAmount;
  private javax.swing.JSpinner spAmount;
  private javax.swing.JSpinner spCycle;
  private javax.swing.JSpinner spSamples;
  // End of variables declaration//GEN-END:variables
}
